package game.model;

import com.sghd.common.utils.model.RatioProbability;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 */
public enum ChessType {
    /**龙*/
    DRAGON,//0
    /**房子*/
    HOUSE,//1
    /**水壶*/
    KETTLE,//2
    /**伞*/
    UMBRELLA,//3
    /**书*/
    BOOK,//4
    /**A*/
    CHESS_A,//5
    /**K*/
    CHESS_K,//6
    /**Q*/
    CHESS_Q,//7
    /**J*/
    CHESS_J,//8
    /**WILD*/
    WILD;//9
    /** 最少连线个数*/
    private int limit;
    /** 个数对应倍数*/
    private Map<Integer, Integer> count2Rate = new HashMap<>();

    /**<房间id, 元素出现权重>*/
    private static Map<Integer, RatioProbability<ChessType>> normalRatioForRoom = new HashMap<>();//普通旋转时各元素出现权重
    private static Map<Integer, RatioProbability<ChessType>> freeRatioForRoom = new HashMap<>();//免费旋转时各元素出现权重

    public static void init(Collection<ConfigRoom> configRooms) {
        configRooms.forEach(configRoom -> {
            //元素个数，对应的倍率
            Map<Integer, Map<Integer, Integer>> count2Rate = configRoom.getCount2Rate();
            for (ChessType type : ChessType.values()) {
                if (type == WILD) {
                    continue;
                }
                Map<Integer, Integer> temp = count2Rate.get(type.ordinal());
                temp.forEach((key, val) -> {
                    if (key <= type.limit || type.limit == 0) {
                        type.limit = key;
                    }
                    type.count2Rate.put(key, val);
                });
            }

            //普通旋转元素出现概率
            Integer[] rates = configRoom.getRates();
            Map<ChessType, Integer> ratio = new HashMap<>();
            for (int i = 0; i < rates.length; i++) {
                ratio.put(ChessType.values()[i], rates[i]);
            }
            normalRatioForRoom.put(configRoom.getId(), RatioProbability.valueOf(ratio));
            //免费旋转元素出现概率
            rates = configRoom.getRatesFree();
            ratio = new HashMap<>();
            for (int i = 0; i < rates.length; i++) {
                ratio.put(ChessType.values()[i], rates[i]);
            }
            freeRatioForRoom.put(configRoom.getId(), RatioProbability.valueOf(ratio));

        });
    }

    /**
     * @param roomId
     * @param ignore
     * @return
     */
    public static ChessType getChess(boolean free, int roomId) {
        if (free) {
            return freeRatioForRoom.get(roomId).getResult();
        } else {
            return normalRatioForRoom.get(roomId).getResult();
        }
    }

    public int getLimit() {
        return limit;
    }

    public int getRate(int count) {
        return count2Rate.get(count);
    }
}
